package com.oreilly.ez.service.impl;

import com.oreilly.ez.entity.SysNote;
import com.oreilly.ez.entity.SysNoteItem;
import com.oreilly.ez.repo.SysNoteItemRepo;
import com.oreilly.ez.repo.SysNoteRepo;
import com.oreilly.ez.service.SysNoteService;
import com.oreilly.ez.util.CloneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class SysNoteServiceImpl  implements SysNoteService{


    @Autowired
    private SysNoteRepo sysNoteRepo;

    @Autowired
    private SysNoteItemRepo sysNoteItemRepo;

    @Override
    public SysNote save(SysNote item) {

        // is update ??
        Integer sid = item.getSid();
        if(sid != null){
            SysNote dbItem = sysNoteRepo.getOne(sid);

            CloneUtils.copyProperties(item, dbItem);
            CloneUtils.copyProperties(dbItem, item);

        }

        return sysNoteRepo.save(item);

    }

    @Override
    public void remove(Integer sid) {

        sysNoteRepo.deleteById(sid);
    }


    @Override
    public Page<SysNote> find(SysNote item, Pageable pageable) {

        return sysNoteRepo.findAll(pageable);
    }

    @Override
    public Object findItems(Integer id) {
        return sysNoteItemRepo.findItems(id);
    }

    @Override
    public Object saveItem(SysNoteItem item) {
        return sysNoteItemRepo.save(item);
    }


    @Override
    public SysNote findOne(Integer sid) {

        return sysNoteRepo.findById(sid).get();
    }


}
